package org.example.jdk8;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;


/**
 * @author zhangjg
 * @desc JDK8
 * @date 2022/01/01 10:14
 **/
public class J8Test1 {

    private static final Logger log = LoggerFactory.getLogger(J8Test1.class);

    /**
     * Lambda表达式测试
     */
    public void t1(){
        String x = "a";
        String y = "b";
        new Thread(() -> {
            while (true) {
                log.info("x={},y={}", x, y);
                try {
                    Thread.sleep(TimeUnit.SECONDS.toMillis(1));
                } catch (InterruptedException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }).start();

    }



    public static void main(String[] args) {

        J8Test1 j8Test1 = new J8Test1();
        j8Test1.t2();

    }


    public void t2(){
        ForkJoinPool forkJoinPool = new ForkJoinPool(2);
        for(int i=0;i<2;i++){
            forkJoinPool.submit(() -> {
                log.info("hello world start");
                try {
                    TimeUnit.SECONDS.sleep(5);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                log.info("hello world end");
            });
            try {
                TimeUnit.SECONDS.sleep(1);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }

        try {
            forkJoinPool.shutdown();
            forkJoinPool.awaitTermination(3, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("。。。。。。。end");

    }



}
